<template>
  <div id="GlobalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <!--点击调整到首页-->
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.jpg" alt="logo" />
            <div class="title">智能云图库</div>
          </div>
        </router-link>
      </a-col>

      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>

      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <!-- 将头像信息和 -->
              <ASpace class="aspace">
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </ASpace>
              <!--添加悬停事件-->
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout"> <LogoutOutlined />退出登录 </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'

//  原始菜单
const originMenus = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  }
]

//  过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus.filter((menu) => {
    if (menu) {
      if (menu.key.startsWith('/admin')) {
        const loginUser = loginUserStore.loginUser
        if (!loginUser || loginUser.userRole != 'admin') {
          return false
        }
      }
      return true
    }
  })
}

const items = computed(() => filterMenus(originMenus))

import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { userLogoutUsingPost } from '@/api/userController'
import checkAccess from '@/access/checkAccess'
const router = useRouter()

// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}

// 当前要高亮的菜单项
const current = ref<string[]>(['/'])
//  监听菜单项变化，并且高亮
router.afterEach((to, from, next) => {
  current.value = [to.path]
})

/**
 * 注销
 */
// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  console.log(res)
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

const loginUserStore = useLoginUserStore()
loginUserStore.fetchLoginUser()
</script>

<style scoped>
#GlobalHeader {
}

#GlobalHeader .title-bar {
  display: flex;
  align-items: center;
}

#GlobalHeader .title {
  color: black;
  font-size: 18px;
  margin-left: 18px;
}

#GlobalHeader .logo {
  height: 48px;
}

#GlobalHeader .aspace {
  font-size: 18px;
}
</style>
